package com.example.springboot_study.service.impl;

import com.example.springboot_study.dao.primary.JtaPrimaryMapper;
import com.example.springboot_study.dao.secondary.JtaSecondaryMapper;
import com.example.springboot_study.entity.primary.User;
import com.example.springboot_study.service.JtaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : liqun
 * @createTime : 2021/9/22 15:21
 */
@Service
public class JtaServiceImpl implements JtaService {
    @Autowired
    private JtaPrimaryMapper jtaPrimaryMapper;
    @Autowired
    private JtaSecondaryMapper jtaSecondaryMapper;

    @Transactional
    @Override
    public void tx(String name,Integer age) {
        // 修改主库中的数据
        int code1 = jtaPrimaryMapper.update(name, age);
        // 修改副库中的数据
        int code2 = jtaSecondaryMapper.update(name, age);
    }

    @Transactional
    @Override
    public void tx2(String name,Integer age) {
        // 修改test1库中的数据
        jtaPrimaryMapper.update(name, age);
        // 模拟：修改test2库之前抛出异常
        throw new RuntimeException();
    }

    public List<User> query1(String name){
        return jtaPrimaryMapper.query(name);
    }

    public List<User> query2(String name){
        return jtaSecondaryMapper.query(name);
    }
}
